import Base from 'models/base';

import SystemUtil from 'utils/systemUtil';

export default class GoodsBuyRecord extends Base {
  constructor() {
    super();

    this.goodsRecord = {};
    this.recordGroup = {};
    this.goodsModelIds = [];
  }

  buyGoods(goodsModelData, goods) {
    let curModel = goodsModelData.curModel;
    let buyCount = goodsModelData.buyCount;
    let modelId = curModel.id;

    let goodsModelIds = this.goodsModelIds;
    let index = goodsModelIds.indexOf(modelId);
    if (index > -1) {
      goodsModelIds.splice(index, 1);
    }

    if (buyCount === 0) {
      this.delGoodModel(curModel, goods);
      return;
    }
    goodsModelIds.push(modelId);

    let data = {
      defId: goods.defId,
      modelId: modelId,
      buyCount: buyCount,
      price: curModel.price,
      sourcePrice: curModel.sourcePrice
    };

    let recordGroup = this.recordGroup;
    recordGroup[modelId] = data;

    let goodsRecord = this.goodsRecord;
    let typeIds = goods.typeIds;
    for (let i = 0; i < typeIds.length; i++) {
      const typeId = typeIds[i];
      let record = goodsRecord[typeId];
      if (record === undefined) {
        record = {};
        goodsRecord[typeId] = record;
      }
      record[modelId] = data;
    }
  }

  delGoodModel(curModel, goods) {
    let modelId = curModel.id;
    let recordGroup = this.recordGroup;
    if (recordGroup[modelId] !== undefined) {
      delete recordGroup[modelId];
    }

    let goodsRecord = this.goodsRecord;
    let typeIds = goods.typeIds;
    for (let i = 0; i < typeIds.length; i++) {
      const typeId = typeIds[i];
      let record = goodsRecord[typeId];
      if (record === undefined) {
        continue;
      }

      if (record[modelId] !== undefined) {
        delete record[modelId];
      }
    }
  }

  getRecordByModelId(modelId) {
    let recordGroup = this.recordGroup;
    let record = recordGroup[modelId];
    if (record === undefined) {
      return null;
    }
    return record;
  }

  getGoodsList() {
    let recordGroup = this.recordGroup;
    return Object.values(recordGroup);
  }

  getStatData() {
    let goodsList = this.getGoodsList();
    let totalPrice = 0,
      totalSourcePrice = 0,
      totalCount = 0;
    for (let i = 0; i < goodsList.length; i++) {
      const goods = goodsList[i];
      let count = goods.buyCount;
      totalPrice += count * goods.price;
      totalSourcePrice += count * goods.sourcePrice;
      totalCount += goods.buyCount;
    }

    totalPrice = SystemUtil.roundValue(totalPrice, 2);
    totalSourcePrice = SystemUtil.roundValue(totalSourcePrice, 2);
    return {
      totalCount,
      totalPrice,
      totalSourcePrice
    };
  }

  getGoodsCount(goods) {
    let modelIds = goods.modelIds;
    let recordGroup = this.recordGroup;
    let count = 0;
    for (let i = 0; i < modelIds.length; i++) {
      let modelId = modelIds[i];
      let data = recordGroup[modelId];
      if (data !== undefined) {
        count += data.buyCount;
      }
    }
    return count;
  }

  getTypeCountData() {
    let goodsRecord = this.goodsRecord;
    let countData = {};
    for (const typeId in goodsRecord) {
      let data = goodsRecord[typeId];
      data = Object.values(data);
      let count = 0;
      for (let i = 0; i < data.length; i++) {
        const obj = data[i];
        count += obj.buyCount;
      }
      countData[typeId] = count;
    }
    return countData;
  }

  getRecordCountByTypeId(typeId) {
    let goodsRecord = this.goodsRecord;
    let data = goodsRecord[typeId];
    if (data === undefined) {
      return 0;
    }

    let count = 0;
    for (let i = 0; i < data.length; i++) {
      const obj = data[i];
      count += obj.buyCount;
    }
    return count
  }
}
